package Micelles;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

public class BinaryCreator {

	
	BufferedImage image;
	
	public BinaryCreator(BufferedImage i){
		
		image = i;
		
		return;
	}
	
	public BufferedImage binary(File location, String name){
		
		int width = image.getWidth();
		int height = image.getHeight();
		
		BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		
		/*Set<String> set = new HashSet<String>();
		Hashtable<String,Integer> table = new Hashtable<String,Integer>();*/
		
		int[] freqs = new int[256];
		
		for(int index = 0; index<256; index++){
			freqs[index] = 0;
		}
		
		int argbInt = 0;
		int red = 0;
		int green = 0;
		int blue = 0;
		
		for(int j = 0; j < height; j++){
			for(int i = 0; i < width; i++){
				
				argbInt = image.getRGB(i, j);
				red = (argbInt >> 16) & 0xff;
			    green = (argbInt >> 8) & 0xff;
			    blue = (argbInt) & 0xff;
			    
				freqs[red] = freqs[red] + 1;
				
			}
		}
		
		File file = new File(location.getAbsolutePath(), name + ".txt");
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				System.out.println("Cannot create file.");
			}
		}
		try {
			BufferedWriter output = new BufferedWriter(new FileWriter(file));
			for(int index = 0; index < 256; index++){
				output.write(index + ", " + freqs[index]);
				output.newLine();
			}
			output.close();
		} catch (IOException e) {
			System.out.println("Can't find the file.");
		}
		
		//Determine Threshold
		
		int total = width*height - freqs[0] - freqs[255];
		int index = 0;
		boolean go = true;
		int cumulative = 0;
		int threshold1 = 0;
		int threshold2 = 0;
		while(go){
			index++;
			cumulative = freqs[index] + cumulative;
			if(cumulative >= total/3 && threshold1 == 0){
				threshold1 = index;
			}
			else if(cumulative >= 2*total/3 && threshold2 == 0){
				threshold2 = index;
				go = false;
			}
		}
//		int threshold = index;
		System.out.println(threshold1);
		System.out.println(threshold2);
		
		//Binary Image Here
		
		for(int j = 0; j < height; j++){
			for(int i = 0; i < width; i++){
				
				argbInt = image.getRGB(i, j);
				red = (argbInt >> 16) & 0xff;
				
				if(red <= threshold1){
					newImage.setRGB(i, j, Color.WHITE.getRGB());
				}
				else if(red > threshold1 && red <= threshold2){
					newImage.setRGB(i, j, Color.BLACK.getRGB());
				}
				else{
					newImage.setRGB(i, j, Color.BLACK.getRGB());
				}
			}
		}
		
		return newImage;
	}
	
	public void saveImage(File location, String name, BufferedImage i){

		try {
			File rendered = new File(location.getAbsolutePath(), name + ".png");
			ImageIO.write(i, "png", rendered);
		} catch (IOException e) {
			System.out.println(e);
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		BufferedImage i= null;
		File startingImage = new File("cropped.png");
		try{
			i = ImageIO.read(startingImage);
		} catch(Exception e){
			System.out.println(e);
		}
		
		BinaryCreator b = new BinaryCreator(i);
		JFileChooser saveChooser = new JFileChooser();
		saveChooser.setDialogType(JFileChooser.SAVE_DIALOG);
		saveChooser.showSaveDialog(new JPanel());
		saveChooser.setDialogTitle("Save Location");
		
		BufferedImage img = b.binary(saveChooser.getCurrentDirectory(), "data");
		b.saveImage(saveChooser.getCurrentDirectory(), "image1", img);
		return;

	}

}
